﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PHTShop.admin
{
    public partial class paypalinfo : System.Web.UI.Page
    {
        private DataContextDataContext dc = new DataContextDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "Paypal information";
            if (!IsPostBack)
            {
                Repeater1.DataSource = dc.Settings.Where(z => z.type.Equals("Paypal Info") && z.name != "Live");
                Repeater1.DataBind();

                var live = dc.Settings.SingleOrDefault(z=>z.type.Equals("Paypal Info") && z.name.Equals("Live"));
                if (live.value.Equals("0"))
                {
                    CheckBox1.Checked = true;
                }
                else {
                    CheckBox1.Checked = false;
                }
                HiddenFieldLive.Value = live.id.ToString();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                foreach (RepeaterItem item in Repeater1.Items)
                {
                   HiddenField hf = item.FindControl("HiddenField1") as HiddenField;
                   TextBox textbox = item.FindControl("TextBox1") as TextBox;
                   int id = Convert.ToInt32(hf.Value);

                   var setting = dc.Settings.SingleOrDefault(z=>z.id==id);
                   setting.value = textbox.Text;

                   dc.SubmitChanges();
                   


                   
                }
                var live = dc.Settings.SingleOrDefault(z => z.type.Equals("Paypal Info") && z.name.Equals("Live"));
                if (CheckBox1.Checked)
                {
                    live.value = "0";
                }
                else
                {
                    live.value = "1";
                }

                dc.SubmitChanges();
                Response.Redirect("Index.aspx");
            }
        }
    }
}